package com.game.sprite.components;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.game.animation.AnimationObject;
import com.game.basic.BasicView;
import com.game.basic.BasicRegion;
import com.game.sprite.BasicSprite;

/**
* @author oven:
* @version 创建时间：2012-11-5 下午3:53:34
* 
*/
public abstract class BasicButton extends BasicSprite{
	public Bitmap[] bg;
	public Bitmap[] text;
	public boolean pressing=false;
	//是否点击后不等待释放就执行动作
	public boolean clickResponse=false;
	
	public BasicButton(Bitmap[] bg,Bitmap[] text,float x,float y){
		float width=bg[0].getWidth();
		float height=bg[0].getHeight();
		this.bg=bg;
		this.text=text;
		basicRegion.setWithSR(x,y,width,height);
		drawRegion=false;
		drawCollectionRegion=false;
		collisionPerc=1;//100%点中
	}
	
	public void paint(Canvas canvas) {
		if(bg.length>1){
			canvas.drawBitmap(pressing?bg[1]:bg[0], basicRegion.getPostx(),basicRegion.getPosty(),paint);
		}else{
			canvas.drawBitmap(bg[0], basicRegion.getPostx(),basicRegion.getPosty(),paint);
		}
		if(text!=null){
			canvas.drawBitmap(pressing?text[1]:text[0],
							basicRegion.getPostx()+(bg[0].getWidth()-text[0].getWidth())/2,
							basicRegion.getPosty()+(bg[0].getHeight()-text[0].getHeight())/2,
							paint);
		}
	}
	
	/**
	 * 点击
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean press(float x,float y){
		if(touchAble&&isTouchInRegion(x,y)){
			if(clickResponse){
				dealPress();
				pressing=false;
			}else{
				pressing=true;
			}
		}
		return pressing;
	}
	
	
	/**
	 * 释放
	 * @param x
	 * @param y
	 */
	public void release(float x,float y){
		if(pressing){
			pressing=false;
			if(isTouchInRegion(x,y)){
					dealPress();
			}
		}
	}
	
	public void setIsPress(boolean value){
		pressing=value;
	}
	
	
	public abstract void dealPress();
}

